﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SQLite;

public partial class SaleInfo_AuditInfo : UDCPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["Reported"]))
            {
                ShowList();
            }
            ReportDataBind();
            NoReportedDataBind();
            if (!String.IsNullOrEmpty(Request.QueryString["Audit"]) || HaveAudited())
            {
                auditAll.Visible = false;
                msg.Text = "昨日共计"+ AuditSaleInfo().ToString() +"户终端通过审核!";
            }
            if (!IsAuditor())
            {
                auditAll.Visible = false;
            }
        }
    }

    private void ReportDataBind()
    {
        string sqlstr = @"select t1.did,t2. dname,ifnull(t3.sales,0) sales
                          from ud_reported_t t1 
                          left join ud_depart_t t2
                            on t1.did = t2.did     
                          left join (select count(serviceId) as sales,did from ud_saleinfo_t t1 left join ud_developer_t t2 on t1.dvid = t2.dvid
                                    where date(saledate) = date('now','localtime','-1 day')   group by t2.did) t3 
                            on t1.did = t3.did
                         where date(repdate) = date('now','localtime','-1 day') 
                         order by t3.sales desc";
        DataSet ds = SQLiteHelper.ExecuteDataset(sqlstr, null);
        ReportList.DataSource = ds;
        ReportList.DataBind();
        SetTotal(ds.Tables[0].Rows.Count);
    }

    private void NoReportedDataBind()
    {
        string sqlstr = @"select t1.dname
                            from ud_depart_t t1  
                           where t1.ismanage = 0 
                             and not exists(select 1 from ud_reported_t t2 
                                             where t1.did = t2.did
                                               and date(t2.repdate) = date('now','localtime','-1 day')) ";
        DataSet ds = SQLiteHelper.ExecuteDataset(sqlstr, null);
        NoReportList.DataSource = ds;
        NoReportList.DataBind();
    }

    private void ShowList()
    {
        if (Request.QueryString["Reported"].ToString().Equals("YES"))
        {
            ReportList.Visible = true;
            NoReportList.Visible = false;
            RepedLink.Attributes.Add("class", "ui-btn-active ui-state-persist");
            NoRepLink.Attributes.Remove("class");
        }
        else
        {
            NoReportList.Visible = true;
            ReportList.Visible = false;
            NoRepLink.Attributes.Add("class", "ui-btn-active ui-state-persist");
            RepedLink.Attributes.Remove("class");
        }
    }

    private void SetTotal(int num)
    {
        StringBuilder total = new StringBuilder();
        total.Append(System.DateTime.Now.AddDays(-1).ToShortDateString());
        total.Append("已报");
        total.Append(num.ToString());
        total.Append("家,发展合约终端新用户");
        DataRow dr = GetCount();
        total.Append(dr["isnew"].ToString());
        total.Append("户，老用户");
        total.Append(dr["isold"].ToString());
        total.Append("户");
        if (!dr["noContract"].ToString().Equals("0"))
        {
            total.Append(",购机入网");
            total.Append(dr["noContract"].ToString());
            total.Append("户");
        }

        total.Append("。 2012@UDC");
        totalCount.InnerText = total.ToString();
    }

    private static DataRow GetCount()
    {
        string sqlstr = @"select sum(case iscontract when 0 then 1 else 0 end) as noContract
                                 ,sum(case when iscontract=1 and isnew=1 then 1 else 0 end) as isnew
                                 ,sum(case when iscontract=1 and isnew=0 then 1 else 0 end) as isold
                            from ud_saleinfo_t t1
                           where date(t1.saledate) = date('now','localtime','-1 day') 
                             and exists(select 1 from ud_developer_t t2 
                                         where t1.dvid = t2.dvid
                                           and exists(select 1 from ud_reported_t t3 
                                                       where t2.did = t3.did
                                                         and date(t3.repdate) = date('now','localtime','-1 day')))";
        DataRow dr = SQLiteHelper.ExecuteDataRow(sqlstr, null);
        return dr;
    }

    private int AuditSaleInfo()
    {
        string sqlstr = @"update ud_saleinfo_t set isaudit = 1
 where date(saledate) = date('now','localtime','-1 day') 
   and exists(select 1 from ud_developer_t t2 
               where dvid = t2.dvid
                 and exists(select 1 from ud_reported_t t3 
                             where t2.did = t3.did
                               and date(t3.repdate) = date('now','localtime','-1 day')));";
        return SQLiteHelper.ExecuteNonQuery(sqlstr, null);
    }
}